package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.models.Shop;
import com.yunchang.springboot.models.mwsreportbean.MwsMonthlyStorageFees;
import com.yunchang.springboot.models.mwsreportbean.MwsMonthlyStorageFeesDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:48
 * @description:
 */
@Component
public class MonthlyStorageFeesDataDispose extends ReportDataDisposeParent implements IReportDataDispose {
    /**
     * 月储存费用   将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        int monthOfChargeIndex = Integer.valueOf(String.valueOf(titleMap.get("month-of-charge")));
        //获取数据库里面最新的日期
//        String latestDay = mwsMonthlyStorageFeesMapper.getLatestDay(sellerId, area);
//        if (!StringUtil.isBlank(latestDay)) {
//            List<String> firstList = lists.get(1);
//            //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
//            if (DateUtil.compateTime(latestDay, firstList.get(monthOfChargeIndex)) >= 0) {
//                return null;
//            }
//        }
        int asinIndex = Integer.valueOf(String.valueOf(titleMap.get("asin")));
        int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
        int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("product-name")));
        int fulfillmentCenterIndex = Integer.valueOf(String.valueOf(titleMap.get("fulfillment-center")));
        int countryCodeIndex = Integer.valueOf(String.valueOf(titleMap.get("country-code")));
        int longestSideIndex = Integer.valueOf(String.valueOf(titleMap.get("longest-side")));
        int medianSideIndex = Integer.valueOf(String.valueOf(titleMap.get("median-side")));
        int shortestSideIndex = Integer.valueOf(String.valueOf(titleMap.get("shortest-side")));
        int measurementUnitsIndex = Integer.valueOf(String.valueOf(titleMap.get("measurement-units")));
        int weightIndex = Integer.valueOf(String.valueOf(titleMap.get("weight")));
        int weightUnitsIndex = Integer.valueOf(String.valueOf(titleMap.get("weight-units")));
        int itemVolumeIndex = Integer.valueOf(String.valueOf(titleMap.get("item-volume")));
        int volumeUnitsIndex = Integer.valueOf(String.valueOf(titleMap.get("volume-units")));
        int averageQuantityOnHandIndex = Integer.valueOf(String.valueOf(titleMap.get("average-quantity-on-hand")));
        int averageQuantityPendingRemovalIndex = Integer.valueOf(String.valueOf(titleMap.get("average-quantity-pending-removal")));
        int estimatedTotalItemVolumeIndex = Integer.valueOf(String.valueOf(titleMap.get("estimated-total-item-volume")));
        int storageRateIndex = Integer.valueOf(String.valueOf(titleMap.get("storage-rate")));
        int currencyIndex = Integer.valueOf(String.valueOf(titleMap.get("currency")));
        int estimatedMonthlyStorageFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("estimated-monthly-storage-fee")));
        int productSizeTierIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("product-size-tier")) {
            productSizeTierIndex = Integer.valueOf(String.valueOf(titleMap.get("product-size-tier")));
        }
        int dangerousGoodsStorageTypeIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("dangerous-goods-storage-type")) {
            dangerousGoodsStorageTypeIndex = Integer.valueOf(String.valueOf(titleMap.get("dangerous-goods-storage-type")));
        }
        int categoryIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("category")) {
            categoryIndex = Integer.valueOf(String.valueOf(titleMap.get("category")));
        }
        int eligibleForInventoryDiscountIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("eligible-for-inventory-discount")) {
            eligibleForInventoryDiscountIndex = Integer.valueOf(String.valueOf(titleMap.get("eligible-for-inventory-discount")));
        }
        int qualifiesForInventoryDiscountIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("qualifies-for-inventory-discount")) {
            qualifiesForInventoryDiscountIndex = Integer.valueOf(String.valueOf(titleMap.get("qualifies-for-inventory-discount")));
        }
        List<Object> insertList = new ArrayList<>(lists.size());
        for (int i = 1; i < lists.size(); i++) {
            MwsMonthlyStorageFeesDO mwsMonthlyStorageFees = new MwsMonthlyStorageFeesDO();
            List<String> row = lists.get(i);
            mwsMonthlyStorageFees.setSellerId(sellerId);
            mwsMonthlyStorageFees.setArea(area);
            mwsMonthlyStorageFees.setAsin(row.get(asinIndex));
            mwsMonthlyStorageFees.setFnsku(row.get(fnskuIndex));
            mwsMonthlyStorageFees.setProductName(row.get(productNameIndex));
            mwsMonthlyStorageFees.setFulfillmentCenter(row.get(fulfillmentCenterIndex));
            mwsMonthlyStorageFees.setCountryCode(row.get(countryCodeIndex));
            mwsMonthlyStorageFees.setLongestSide(new BigDecimal(String.valueOf(row.get(longestSideIndex))));
            mwsMonthlyStorageFees.setMedianSide(new BigDecimal(String.valueOf(row.get(medianSideIndex))));
            mwsMonthlyStorageFees.setShortestSide(new BigDecimal(String.valueOf(row.get(shortestSideIndex))));
            mwsMonthlyStorageFees.setMeasurementUnits(row.get(measurementUnitsIndex));
            mwsMonthlyStorageFees.setWeight(new BigDecimal(String.valueOf(row.get(weightIndex))));
            mwsMonthlyStorageFees.setWeightUnits(row.get(weightUnitsIndex));
            mwsMonthlyStorageFees.setItemVolume(new BigDecimal(String.valueOf(row.get(itemVolumeIndex))));
            mwsMonthlyStorageFees.setVolumeUnits(row.get(volumeUnitsIndex));
            mwsMonthlyStorageFees.setAverageQuantityOnHand(new BigDecimal(String.valueOf(row.get(averageQuantityOnHandIndex))));
            mwsMonthlyStorageFees.setAverageQuantityPendingRemoval((row.get(averageQuantityPendingRemovalIndex)));
            mwsMonthlyStorageFees.setEstimatedTotalItemVolume(new BigDecimal(String.valueOf(row.get(estimatedTotalItemVolumeIndex))));
            mwsMonthlyStorageFees.setMonthOfCharge(row.get(monthOfChargeIndex));
            mwsMonthlyStorageFees.setStorageRate(new BigDecimal(String.valueOf(row.get(storageRateIndex))));
            mwsMonthlyStorageFees.setCurrency(row.get(currencyIndex));
            mwsMonthlyStorageFees.setEstimatedMonthlyStorageFee(new BigDecimal(String.valueOf(row.get(estimatedMonthlyStorageFeeIndex))));
            mwsMonthlyStorageFees.setCreateTime(DateUtil.getDate());
            mwsMonthlyStorageFees.setUpdateTime(DateUtil.getDate());
            if (productSizeTierIndex != WRONG_NUMBER_SIGN) {
                mwsMonthlyStorageFees.setProductSizeTier(row.get(productSizeTierIndex));
            }
            if (dangerousGoodsStorageTypeIndex != WRONG_NUMBER_SIGN) {
                mwsMonthlyStorageFees.setDangerousGoodsStorageType(row.get(dangerousGoodsStorageTypeIndex));
            }
            if (categoryIndex != WRONG_NUMBER_SIGN) {
                mwsMonthlyStorageFees.setCategory(row.get(categoryIndex));
            }
            if (eligibleForInventoryDiscountIndex != WRONG_NUMBER_SIGN) {
                mwsMonthlyStorageFees.setEligibleForInventoryDiscount(row.get(eligibleForInventoryDiscountIndex));
            }
            if (qualifiesForInventoryDiscountIndex != WRONG_NUMBER_SIGN) {
                mwsMonthlyStorageFees.setQualifiesForInventoryDiscount(row.get(qualifiesForInventoryDiscountIndex));
            }
            mwsMonthlyStorageFees.setFinanceStatus(1);
            insertList.add(mwsMonthlyStorageFees);
        }
        return insertList;
    }
}
